#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_dictionary.py
# author:wmf
# datetime:2021/3/11 14:29
# software: PyCharm
'''
this is function description
'''
# import module your need

#字典是一个无序的数据集合，使用print函数输出字典时，通常输出的顺序和定义的顺序时不一致的!
xiaoming = {"name": "小明",
            "age": 18,
            "gender": True,
            "height": 1.75,
            "weight": 130}

print(xiaoming)

xiaoming_dict = {"name": "小明",
                 "gender": True}

#1.取值
print(xiaoming_dict["name"])
#在取值的时候，如果指定的key不存在，程序会报错!
#print(xiaoming_dict["name123"])

#2.增加/修改
xiaoming_dict["age"] = 18
#如果key存在，会修改已经存在的键值对
xiaoming_dict["name"] = "小小明"
print(xiaoming_dict)

#3.删除
xiaoming_dict.pop("name")
print(xiaoming_dict)

#4.统计键值对数量
print(len(xiaoming_dict))

#5.合并字典
temp_dict = {"height":1.75,
             "age":20}
xiaoming_dict.update(temp_dict)
print(xiaoming_dict)

#6.清空字典
xiaoming_dict.clear()

print(xiaoming_dict)


xiaoming1_dict = {"name": "小明",
                  "qq": "123456",
                  "phone": "10086"}

#迭代遍历字典
#变量k是每一次循环中，获取到的键值对的key
for k in xiaoming1_dict:
    print("%s: %s" %(k,xiaoming1_dict[k]))

#将多个字典放在一个列表中，再进行遍历
card_list = [
    {"name": "张三",
     "qq": "12345",
     "phone": "110"},
    {"name": "李四",
     "qq": "54321",
     "phone": "10086"}
]

for card_info in card_list:
    print(card_info)